﻿﻿﻿/*!
* 网页日历，带有公历和农历节日与节气
*/

var Calendar_itinerary; //日历数据的全局变量
function Calendar() {
    var $id = 0, $url = "javascript:void(0);";
    var $this = this;
    var $date = new Date();
    var $weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
    var $days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

    var SolarTermStr = new Array(
                        "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至",
                        "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至"); //24节气
    var DifferenceInMonth = new Array(
                        1272060, 1275495, 1281180, 1289445, 1299225, 1310355, 1321560, 1333035, 1342770, 1350855, 1356420, 1359045,
                        1358580, 1355055, 1348695, 1340040, 1329630, 1318455, 1306935, 1297380, 1286865, 1277730, 1274550, 1271556); //24节气值

    var V = { "0101": "*1元旦", "0214": "情人节", "0305#": "学雷锋纪念日", "0308": "妇女节", "0312#": "植树节", "0315#": "消费者权益日", "0401#": "愚人节", "0501": "*1劳动节", "0504": "青年节", "0601": "儿童节", "0701": "建党节", "0801": "建军节", "0910": "教师节", "1001": "*3国庆节", "1224": "平安夜", "1225": "圣诞节" }; //阳历节日
    var T = { "0101": "*2春节", "0115": "元宵节", "0505": "*1端午节", "0815": "*1中秋节", "0707": "七夕", "0909": "重阳节", "1010#": "感恩节", "1208#": "腊八节", "0100": "除夕" }; //阴历节日

    var regzhongwen = /[A-Za-z_\-\~\!\@\#\$\%\^\&\*\(\)\|\0-9]+/; //过滤特殊字符、英文与数字
    var $reservation_title = ""; //"<span>(点击日历上价格可以进行预订)<span>";

    this.year = $date.getFullYear();
    this.month = $date.getMonth() + 1;
    this.date = $date.getDate();
    var ui; //this.ui = null;
    this.even = false; //是否启用两个日历框
    this.isSolarTerm = true; //是否显示节日、节气

    Calendar.isLeapYear = function(year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }

    this.createUI = function() {
        var grid = document.createElement("TABLE");
        var gridBody = document.createElement("TBODY");
        var btnPrev = document.createElement("A");
        var btnNext = document.createElement("A");

        with (grid) {
            border = "0";
            className = "calendar";
            cellPadding = "0";
            cellSpacing = "0";
        }

        with (btnPrev) {
            href = "javascript:void(0);";
            id = "subPrev";
            innerText = textContent = "";
            var prevIndex = 3;
            onclick = function(e) {
                e = e || window.event;
                var date = new Date($this.year, $this.month - (($this.even || $.trim($(this).attr("even"))) ? 2 : 1), $this.date);
                $this.year = date.getFullYear(); // $this.year;
                $this.month = date.getMonth(); // $this.month;
                $this.date = date.getDate();
                if ($this.month == 0) {
                    $this.year = $this.year - 1;
                    $this.month = 12;
                }
                if ($.trim($(this).attr("even")) == "1") {
                    $(this).parent().parents("div").find("a[id='subPrev']").eq(1).removeAttr("even").click();
                    prevIndex = 4;
                }
                $this.change($this.year, $this.month, $this.date, null, prevIndex);
            };
        }

        with (btnNext) {
            href = "javascript:void(0);";
            id = "subNext";
            innerText = textContent = "";
            var nextIndex = 1;
            onclick = function(e) {
                e = e || window.event;
                var date = new Date($this.year, $this.month + (($this.even || $.trim($(this).attr("even"))) ? 1 : 0), $this.date);
                $this.year = date.getFullYear();
                $this.month = date.getMonth() + 1;
                $this.date = date.getDate();
                if ($this.even) {
                    $(this).parent().parents("div").find("a[id='subNext']").eq(0).attr("even", "1").click();
                    nextIndex = 2;
                }
                $this.change($this.year, $this.month, $this.date, null, nextIndex);
            };
        }

        grid.appendChild(gridBody);
        for (var i = 0; i < 8; ++i) {
            var row = document.createElement("TR");
            switch (i) {
                case 0:
                    row.className = "calendar-title";
                    break;
                case 1:
                    row.className = "calendar-weeks";
                    break;
                default:
                    row.className = "calendar-week-days";
                    break;
            }
            for (var j = 0; j < 7; ++j) {
                var cell = document.createElement("TD");
                cell.style.width = "auto";
                cell.className = (j == 0 || j == 6) ? "calendar_color_ff6600" : ""; //星期日 星期六
                switch (i) {
                    case 0:
                        switch (j) {
                            case 0:
                                cell.appendChild(btnPrev);
                                break;
                            case 1:
                                j = 5;
                                with (cell) {
                                    colSpan = "5";
                                    className = "calendar-title-current-month";
                                }
                                break;
                            default:
                                cell.appendChild(btnNext);
                                break;
                        }
                        break;
                    case 1:
                        cell.innerText = cell.textContent = $weeks[j];
                        break;
                    default:
                        cell.innerText = cell.textContent = " ";
                        break;
                }
                row.appendChild(cell);
            }
            gridBody.appendChild(row);
        }

        return grid;
    }

    this.change = function(year, month, date, frequency, index) {
    	// 请求时间价格数据, ajax请求非异步操作
	    if ((initEven != null && initEven == true) 
	    	&& index != null && index != undefined 
	    	&& index == 1 
			&& typeof(refreshDateAndPrice) != 'undefined' && refreshDateAndPrice instanceof Function) {
	    	refreshDateAndPrice(year + "-" +  month + "-" + date, 1);	// 双日期, 向后翻
	    } else if ((initEven != null && initEven == true) 
	    	&& index != null && index != undefined 
	    	&& index == 3 
			&& typeof(refreshDateAndPrice) != 'undefined' && refreshDateAndPrice instanceof Function) {
	    	refreshDateAndPrice(year + "-" +  month + "-" + date, -1);	// 双日期, 向前翻
	    } else if ((initEven == null || initEven == false)
	    	&& index != null && index != undefined 
	    	&& typeof(refreshDateAndPrice) != 'undefined' && refreshDateAndPrice instanceof Function) {
	    	refreshDateAndPrice(year + "-" +  month + "-" + date, 0);	// 单日期
	    }
    	
        var nowDate = new Date(year, month, date);
        var itinerary = Calendar_itinerary; //日历切位对象集合
        var dtime = [year, month]; //初始当前日期为传入日期值，即使ajax未返回查询集合最小值也能正常向下走

//		// 显示从有数据的月份开始	, 前台用
//        if (itinerary && itinerary.data.length >= 1) {
//            dtime = itinerary.data[0].pdatetime.split('-'); //重新给定返回集合的最小日期
//        }        
        
        if (frequency == 1) { //取到第一个对象的日期


            var tempDate = new Date(year, month, 1);
//            if ($(ui).parent("div").find("table").length == 1) {
//                tempDate = new Date(parseInt(dtime[0]), parseInt(dtime[1]), 1); //当前返回数据的最小日期
                year = tempDate.getFullYear(); month = tempDate.getMonth();
//            } else {
//                tempDate = new Date(parseInt($.trim(dtime[0])), parseInt($.trim(dtime[1])) + 1, 1); //取第一个日期加一
//                year = tempDate.getFullYear(); month = tempDate.getMonth();
//            }

            $date = new Date(year, month, 1);
            $this.year = $date.getFullYear();
            $this.month = $date.getMonth();
            $this.date = $date.getDate();
            if ($this.month == 0) {
                $this.year = $this.year - 1;
                $this.month = 12;
                year = $this.year; month = 12;
            }
        } //end if (frequency == 1 && dtime.length >= 2)//首次加载可跨月

        with (ui.rows[0].children[1]) {
            innerText = textContent = year + " 年 " + month + " 月"; //+ (month < 10 ? "0" : "")
            innerHTML = innerText + $reservation_title; //标题
        }

        for (var i = 2; i < ui.rows.length; ++i) {
            for (var j = 0; j < 7; ++j) {
                with (ui.rows[i].children[j]) {
                    innerText = textContent = "";
                }
            }
        } // end for

        var date = new Date(year, month - 1, 1);
        var dayOfWeek = date.getDay();
        var day = 1, days = $days[month - 1] + (month == 2 && Calendar.isLeapYear(year) ? 1 : 0);
        //$this.date = (month == 2 && Calendar.isLeapYear(year)) ? day : $this.date; /*$this.date = day;****/改动1

        for (var i = 2; i < ui.rows.length; ++i) {

            for (var j = i == 2 ? dayOfWeek : 0; j < 7; ++j) {
                var isToday = (year == $date.getFullYear() && month == $date.getMonth() + 1 && day == $date.getDate());
                with (ui.rows[i].children[j]) {
                    className = isToday ? "calendar-today" : "";
                    var dayT = "";
                    if ($this.isSolarTerm == true) {
                        var tdate = new Date(date.getFullYear(), date.getMonth(), day);
                        dayT = T[showCal(tdate, true)]; dayT = dayT ? dayT : ""; //GetCNDate.js showCal(date, lockNum)//以阴历节日为主

                        if (!dayT) {
                            var dayV = V[(month < 10 ? "0" + month : month.toString()) + (day < 10 ? "0" + day : day.toString())]; //阳历节日
                            dayT = dayV ? dayV : "";
                            if (!dayT) {
                                var dayJ = $this.jieqi(tdate); //24节气 如果当前天没有公历与农历节日则判断性取节气
                                if (dayJ) dayT = dayJ ? dayJ : "";
                            }
                        }
                        dayT = dayT ? "<span class='v-holiday'>" + dayT.replace(regzhongwen, '') + "</span>" : ""; //节日text
                    } //end if (this.isSolarTerm == true)  是否显示节日、节气
                    if (itinerary) {
                        for (var itr = 0; itr < itinerary.data.length; itr++) {
                            var otime = itinerary.data[itr].pdatetime.split('-'); //当前服务器的时间
                            var _day = new Date(otime[1] + '/' + otime[2] + '/' + otime[0]).getDate();
                            var _month = new Date(otime[1] + '/' + otime[2] + '/' + otime[0]).getMonth() + 1;
                            var _year = new Date(otime[1] + '/' + otime[2] + '/' + otime[0]).getFullYear();
                            if (_day == day && month == _month && _year == year) {
                                className = isToday ? "calendar-tag" : "";  //表示当前天为今天则改有红色字体及12号字
                                var time1 = new Date(itinerary.data[itr].pdatetime.replace(/-/, "/").replace("-", "/"));
                                var time2 = new Date(new Date().setDate(parseInt(new Date().getDate()) + parseInt(itinerary.data[itr].hour) / 24));

                                var flag = time1 < time2; //判断当前日历日期是否小于当前日期加上提前预定天数
                                if (flag) {
                                    innerHTML = textContext = "<div class='calendar_bg_ffffcc' title='成人价: ￥" + itinerary.data[itr].price + " 儿童价: ￥" + (itinerary.data[itr].childPrice || '--') + " 单房差: ￥" + (itinerary.data[itr].singleRoom || '--') + "'>" + dayT + day + "<div><font class='calendar_color_333'>￥" + itinerary.data[itr].price + "<br/>已满</font></div></div>";
                                }
                                else {
                                    var thhtml = "<div id='d" + itr + "' class='calendar_bg_ffffcc' onclick=\"showdiv('" + year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day) + "');\" title='成人价: ￥" + itinerary.data[itr].price + " 儿童价: ￥" + (itinerary.data[itr].childPrice || '--') + " 单房差: ￥" + (itinerary.data[itr].singleRoom || '--') + "' onmouseover='divchange1(this)' onmouseout='divchange2(this)' >" + dayT + day + "<div><a class='calendar_red'  >￥" + itinerary.data[itr].price + "</a>${text}</div></div>";

                                    if (itinerary.data[itr].price == "-1") {
                                        thhtml = thhtml.replace("${text}", "<br />请电询");
                                    }
                                    else if (itinerary.data[itr].cuteNum == "0") {
                                        thhtml = thhtml.replace("${text}", "<br />实时查询");
                                    }
                                    else if (itinerary.data[itr].cuteNum == "-1") {
                                        thhtml = thhtml.replace("${text}", "");
                                    }
                                    else {
                                        if (itinerary.data[itr].cuteNum > 9) {
                                            thhtml = thhtml.replace("${text}", "<br />余位>9");
                                        } else {
                                            thhtml = thhtml.replace("${text}", "<br />余位:" + itinerary.data[itr].cuteNum);
                                        }
                                    }
                                    textContext = innerHTML = thhtml;
                                } //end else
                            } //end if                                    
                        } //end for
                    } //end if
                    if (innerHTML == "")
                        innerHTML = textContext = "<div>" + dayT + (day) + "</div>"; //innerText = textContent = (isToday ? "今天" : day) + dayV;  

                } // end with
                ++day;
                if (day > days)
                    return;
            } //end for
        } //end for
    }   //**************************************************************** end change ********************************************

    //节气
    this.jieqi = function(date) {
        var DifferenceInYear = 31556926;
        var BeginTime = new Date(1901 / 1 / 1);
        BeginTime.setTime(947120460000);
        for (; date.getFullYear() < BeginTime.getFullYear(); ) {
            BeginTime.setTime(BeginTime.getTime() - DifferenceInYear * 1000);
        }
        for (; date.getFullYear() > BeginTime.getFullYear(); ) {
            BeginTime.setTime(BeginTime.getTime() + DifferenceInYear * 1000);
        }
        for (var M = 0; date.getMonth() > BeginTime.getMonth(); M++) {
            BeginTime.setTime(BeginTime.getTime() + DifferenceInMonth[M] * 1000);
        }
        if (date.getDate() > BeginTime.getDate()) {
            BeginTime.setTime(BeginTime.getTime() + DifferenceInMonth[M] * 1000);
            M++;
        }
        if (date.getDate() > BeginTime.getDate()) {
            BeginTime.setTime(BeginTime.getTime() + DifferenceInMonth[M] * 1000);
            M == 23 ? M = 0 : M++;
        }
        var JQ = "";
        if (date.getDate() == BeginTime.getDate()) {
            JQ += SolarTermStr[M];
        }
        return JQ;
    }

	this.init = function(initData, ele, id, url, iseven) {
		Calendar_itinerary = initData;
		initEven = Boolean(iseven);
		this.show(ele, id, url, iseven);
	}
	
    this.show = function(ele, id, url, iseven) {
        ui = ui || this.createUI();
        this.date = 1; //初始化
        $id = id; $url = url ? url : $url;
        $this.even = Boolean(iseven); //表示为两个日历框

        if (this.even == false && ele) {
            ele.appendChild(ui);
            this.change(this.year, this.month, this.date, 1);
            //$(ele).css({ "filter": "alpha(opacity=0,finishopacity=100,style=1)" }).show().fadeTo(2000, 0.9, function() { $(this).removeAttr("style").find("table").css({ "float": "left" }); }); //滤镜效果
        } //不为双日历时快速填充，否则用下面方法进行填充

        if (this.even && ele) {
            $date = new Date(this.year, this.month, this.date);
            $this.year = $date.getFullYear();
            $this.month = $date.getMonth() + 1;
            $this.date = $date.getDate();
            new Calendar().show(ele, id, url);
            ele.appendChild(ui);
            $this.change($this.year, $this.month, $this.date, 1);
            $(ele).find("a[id='subPrev']").eq(1).css("display", "none");
            $(ele).find("a[id='subNext']").eq(0).css("display", "none");

            $(ele).find("a[id='subPrev']").eq(0).attr("even", "1");
            $(ele).find("a[id='subNext']").eq(1).css("even", "1");
            $(ele).find("table").css({ "float": "left" });
        }
    }

//    this.toString = function() {
//        alert(this.year + "-" + this.month + "-" + this.date);
//    }
}